package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldComplaints;
import com.cleaningcloud.cld.domain.CldComplaintsReply;
import com.cleaningcloud.cld.mapper.CldComplaintsMapper;
import com.cleaningcloud.cld.mapper.CldComplaintsReplyMapper;
import com.cleaningcloud.cld.service.ICldComplaintsReplyService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客诉回复Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-17
 */
@Service
public class CldComplaintsReplyServiceImpl implements ICldComplaintsReplyService {
    @Autowired
    private CldComplaintsReplyMapper cldComplaintsReplyMapper;

    @Autowired
    private CldComplaintsMapper cldComplaintsMapper;

    /**
     * 查询客诉回复
     *
     * @param complanintsReplyId 客诉回复主键
     * @return 客诉回复
     */
    @Override
    public CldComplaintsReply selectCldComplaintsReplyByComplanintsReplyId(Long complanintsReplyId) {
        return cldComplaintsReplyMapper.selectCldComplaintsReplyByComplanintsReplyId(complanintsReplyId);
    }

    /**
     * 查询客诉回复列表
     *
     * @param cldComplaintsReply 客诉回复
     * @return 客诉回复
     */
    @Override
    public List<CldComplaintsReply> selectCldComplaintsReplyList(CldComplaintsReply cldComplaintsReply) {
        return cldComplaintsReplyMapper.selectCldComplaintsReplyList(cldComplaintsReply);
    }

    /**
     * 新增客诉回复
     *
     * @param cldComplaintsReply 客诉回复
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldComplaintsReply(CldComplaintsReply cldComplaintsReply) {
        cldComplaintsReply.setReplyTime(DateUtils.getNowDate());
        cldComplaintsReply.setReplyStaffId(SecurityUtils.getUserId());
        // 修改客诉状态为已反馈
        CldComplaints complaints = cldComplaintsMapper.selectCldComplaintsByComplaintsId(cldComplaintsReply.getComplanintsId());
        if (complaints != null) {
            if (complaints.getHandleStatus() == 1 || complaints.getHandleStatus() == 0) {
                complaints.setHandleStatus(2L);
                complaints.setHandleTime(DateUtils.getNowDate());
                complaints.setHandleStaffId(complaints.getHandleStaffId() == null ? SecurityUtils.getUserId() : complaints.getHandleStaffId());
                complaints.setAssignStaffId(complaints.getAssignStaffId() == null ? SecurityUtils.getUserId() : complaints.getAssignStaffId());
                cldComplaintsMapper.updateCldComplaints(complaints);
            }
        }
        return cldComplaintsReplyMapper.insertCldComplaintsReply(cldComplaintsReply);
    }

    /**
     * 修改客诉回复
     *
     * @param cldComplaintsReply 客诉回复
     * @return 结果
     */
    @Override
    public int updateCldComplaintsReply(CldComplaintsReply cldComplaintsReply) {
        return cldComplaintsReplyMapper.updateCldComplaintsReply(cldComplaintsReply);
    }

    /**
     * 批量删除客诉回复
     *
     * @param complanintsReplyIds 需要删除的客诉回复主键
     * @return 结果
     */
    @Override
    public int deleteCldComplaintsReplyByComplanintsReplyIds(Long[] complanintsReplyIds) {
        return cldComplaintsReplyMapper.deleteCldComplaintsReplyByComplanintsReplyIds(complanintsReplyIds);
    }

    /**
     * 删除客诉回复信息
     *
     * @param complanintsReplyId 客诉回复主键
     * @return 结果
     */
    @Override
    public int deleteCldComplaintsReplyByComplanintsReplyId(Long complanintsReplyId) {
        return cldComplaintsReplyMapper.deleteCldComplaintsReplyByComplanintsReplyId(complanintsReplyId);
    }
}
